package com.meng.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;


/**
 * 自定义扫描器  扫描PublishNettyServer注解
 *
 * @author MENG
 * @version 2018/3/20
 * @see
 */
@Component
public class PublishNettyServerScanner implements BeanFactoryPostProcessor, ApplicationContextAware
{
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        this.applicationContext = applicationContext;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException
    {
        //@PublishThriftServer 扫描器 通过继承@component的扫描器 实现扫描自定义注解 加载该注解修饰的Bean
        Scanner scanner = new Scanner((BeanDefinitionRegistry) configurableListableBeanFactory);

        scanner.setResourceLoader(this.applicationContext);

        scanner.scan("com.meng.netty.server");//扫描发布服务的package

    }

    public final static class Scanner extends ClassPathBeanDefinitionScanner
    {
        public Scanner(BeanDefinitionRegistry registry)
        {
            super(registry);
        }

        //重写 ClassPathScanningCandidateComponentProvider  (ClassPathBeanDefinitionScanner继承的类)
        //的registerDefaultFilters 方法实现只扫描 自定义注解
        public void registerDefaultFilters()
        {
            this.addIncludeFilter(new AnnotationTypeFilter(PublishNettyServer.class));
        }
    }
}
